package tech.yunland.shop.domain.model;


import com.moyuer.bsf.domain.model.Organ;
import tech.yunland.activity.domain.imodel.BaseModel;

import javax.persistence.*;
import java.math.BigDecimal;
import java.util.Set;

/**
 * Created by v on 17-5-12.
 */
@Entity
@Table(name = "allot_bill")
public class AllotBill extends Bill implements BaseModel {

    @OneToMany(mappedBy = "owner")
    protected Set<AllotBillEntry> entries; // 分录




    @ManyToOne
    private Organ outStore;//出库仓库
    @ManyToOne
    private Organ inStore ;//入库仓库
    @Transient
    private String outStoreName;
    @Transient
    private String outStoreId;
    @Transient
    private String inStoreName;
    @Transient
    private String inStoreId;
    @Transient
    private BigDecimal amount;

    @Column(name = "processdef_id")
    private String processdefId;//流程id

    @Column(name = "processdef_name")
    private String processdefName;//流程名称

    //最近审批人
    @Column(name = "recently_approval")
    public String recentlyApproval;

    private String result;
    @Column(length = 50)
    private String handerName;

    @Transient
    private Integer entrySize; //实体数量

    @Transient
    private String entryName; //分录商品名称

    public Organ getOutStore() {
        return outStore;
    }

    public String getOutStoreName() {
        if(outStore!=null){
            this.outStoreName = outStore.getName();
        }
        return outStoreName;
    }

    public void setOutStoreName(String outStoreName) {
        this.outStoreName = outStoreName;
    }

    public String getOutStoreId() {
        if(outStore!=null){
            this.outStoreId = outStore.getId();
        }
        return outStoreId;
    }

    public void setOutStoreId(String outStoreId) {
        this.outStoreId = outStoreId;
    }

    public String getInStoreName() {
        if(inStore!=null){
            this.inStoreName = inStore.getName();
        }
        return inStoreName;
    }

    public void setInStoreName(String inStoreName) {
        this.inStoreName = inStoreName;
    }

    public String getInStoreId() {
        if(inStore!=null){
            this.inStoreId = inStore.getId();
        }
        return inStoreId;
    }

    public void setInStoreId(String inStoreId) {
        this.inStoreId = inStoreId;
    }

    public void setOutStore(Organ outStore) {
        this.outStore = outStore;
    }

    public Organ getInStore() {
        return inStore;
    }

    public void setInStore(Organ inStore) {
        this.inStore = inStore;
    }

    public Set<AllotBillEntry> getEntries() {
        return entries;
    }

    public void setEntries(Set<AllotBillEntry> entries) {
        this.entries = entries;
    }








    public BigDecimal getAmount() {
        BigDecimal amount = BigDecimal.ZERO;
        if (entries != null) {
            for (AllotBillEntry entry : entries) {
                if (entry.getState() != null && entry.getState().intValue() == 1) {
                    amount = amount.add(entry.getAmount());
                }
            }
        }
        return amount;
    }

    public void setAmount(BigDecimal amount) {
        this.amount = amount;
    }

    public String getProcessdefId() {
        return processdefId;
    }

    public void setProcessdefId(String processdefId) {
        this.processdefId = processdefId;
    }

    public String getProcessdefName() {
        return processdefName;
    }

    public void setProcessdefName(String processdefName) {
        this.processdefName = processdefName;
    }

    public String getRecentlyApproval() {
        return recentlyApproval;
    }

    public String getResult() {
        return result;
    }

    public String getHanderName() {
        return handerName;
    }

    public void setHanderName(String handerName) {
        this.handerName = handerName;
    }

    public Integer getEntrySize() {
        int i = 0;
        for (AllotBillEntry entry : entries) {
            if (entry.getState() != null && entry.getState().intValue() != -1) {
                i++;
            }
        }
        return Integer.valueOf(i);
    }

    public void setEntrySize(Integer entrySize) {
        this.entrySize = entrySize;
    }

    public String getEntryName() {
        String names = "";
        for (AllotBillEntry entry : entries) {
            if (entry.getState() != null && entry.getState().intValue() != -1) {
                names += entry.getGoodsName() + ",";
            }
        }
        return names.length() > 1 ? names.substring(0, names.length() - 1) : "";
    }

    public void setEntryName(String entryName) {
        this.entryName = entryName;
    }

    @Override
    public void setRecentlyApproval(String s) {

    }

    @Override
    public void setResult(String s) {

    }
}
